package com.itzhoujun.jtimer.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.itzhoujun.jtimer.entity.StockAnalyse;
import com.itzhoujun.jtimer.entity.StockInfo;
import com.itzhoujun.jtimer.mapper.StockAnalyseMapper;
import com.itzhoujun.jtimer.mapper.StockInfoMapper;
import com.itzhoujun.jtimer.utils.Response;
import com.itzhoujun.jtimer.utils.TableSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/StockAnaly")
public class StockAnalyseController {

    @Autowired
    private StockAnalyseMapper stockAnalyseMapper;
    @Autowired
    private StockInfoMapper stockInfoMapper;

    @GetMapping("/index")
    public String index(){
        return "index2";
    }
    
    @PostMapping("/doUpdate")
    @ResponseBody
    public Object doUpdate(@Valid StockAnalyse stock, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Response.error(bindingResult.getFieldError().getDefaultMessage());
        }
        Integer id = stock.getId();
        int num;
        if(id != null && id > 0){
            num = stockAnalyseMapper.updateById(stock);
        }else{

            num = stockAnalyseMapper.insert(stock);

            EntityWrapper<StockInfo> wrapper = new EntityWrapper();
            wrapper.eq("stock_cd", stock.getStockCd());
            if(stockInfoMapper.selectCount(wrapper) == 0 ) {
                StockInfo sinfo = new StockInfo();
                sinfo.setStockCd(stock.getStockCd());
                sinfo.setStockName(stock.getStockName());
                sinfo.setForecast(stock.getForecast());
                stockInfoMapper.insert(sinfo);
            }
        }
        if(num > 0){
            return Response.successAndJump("/StockInfo/index");
        }else{
            return Response.error("操作失败");
        }

    }

    @GetMapping(value = {"/edit","/add"})
    public String edit(Integer id,ModelMap map){
    	System.out.println("id:"+id);
        if(null != id && id > 0){
            map.addAttribute("vo",stockAnalyseMapper.selectById(id));
        }
        return "stockAnaly/edit";
    }


    @GetMapping("/lists")
    @ResponseBody
    public Object lists(Integer page, Integer limit){
        TableSet<StockAnalyse> datasets = new TableSet<>(page,limit);
        List<StockAnalyse> list = stockAnalyseMapper.selectPage(datasets);
        return datasets.setRecords(list).response();
    }

    @GetMapping("/listsByCode")
    @ResponseBody
    public Object listsByCode(Integer page, Integer limit,String stockCd){
        System.out.println("listsByCode"+stockCd);
        TableSet<StockAnalyse> datasets = new TableSet<>(page,limit);

        EntityWrapper<StockAnalyse> wrapper = new EntityWrapper();
        wrapper.eq("stock_cd", stockCd);

        List<StockAnalyse> list = stockAnalyseMapper.selectPage(datasets,wrapper);
//        List<StockAnalyse> list = stockAnalyseMapper.selectPage(datasets);
        return datasets.setRecords(list).response();
    }


    @GetMapping("doDel")
    @ResponseBody
    public Object doDel(Integer id){
        int num = 0;
        if(null != id && id > 0){
            num = stockAnalyseMapper.deleteById(id);
        }
        if(num > 0){
            return Response.success();
        }else{
            return Response.error();
        }
    }
}
